package com.bhs.sansong.ui.order;

import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansong.R;
import com.bhs.sansong.adapter.PictureShowAdapter3;
import com.bhs.sansong.base.BaseFragment;
import com.bhs.sansong.bean.OrderImages;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.utils.MyUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.util.SmartGlideImageLoader;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Response;

public class TransportDocumentFragment extends BaseFragment implements NetCallBack {

    private RecyclerView mRvList;
    private PictureShowAdapter3 showAdapter3;
    private String imgType;
    private RelativeLayout mRlEmpty;

    public static TransportDocumentFragment newInstance(String deliver_sn, String img_type) {
        TransportDocumentFragment upstreamShipmentListFragment = new TransportDocumentFragment();
        Bundle bundle = new Bundle();
        bundle.putString("deliver_sn", deliver_sn);
        bundle.putString("img_type", img_type);
        upstreamShipmentListFragment.setArguments(bundle);
        return upstreamShipmentListFragment;
    }

    @Override
    public int initLayout() {
        return R.layout.fragment_transpor_document;
    }

    @Override
    public void initView(View root) {
        imgType = getArguments().getString("img_type");
        mRvList = root.findViewById(R.id.rv_list);
        networkRequest(netApi.get_order_images(getArguments().getString("deliver_sn"), imgType), this);
        mRlEmpty = root.findViewById(R.id.rl_empty);
    }

    @Override
    public void initData() {

        mRvList.setLayoutManager(new GridLayoutManager(getContext(), 3));

    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        OrderImages images = new Gson().fromJson(json, OrderImages.class);
        if (images.isStatus()) {
            List<Object> list = new ArrayList<>();
            if (imgType.equals("13")) {
                for (OrderImages.DataDTO.ImagesListDTO._$13DTO dto : images.getData().getImages_list().get_$13()) {
                    list.add(dto.getImg_path());
                }
            } else if (imgType.equals("15")) {
                for (OrderImages.DataDTO.ImagesListDTO._$13DTO dto : images.getData().getImages_list().get_$15()) {
                    list.add(dto.getImg_path());
                }
            }
            if (!MyUtils.isEmpty(list)) {
                mRlEmpty.setVisibility(View.GONE);
            }
            showAdapter3 = new PictureShowAdapter3(R.layout.item_picture2, list);
            mRvList.setAdapter(showAdapter3);
            showAdapter3.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    new XPopup.Builder(getContext())
                            .asImageViewer(view.findViewById(R.id.iv_picture), position, list, null, new SmartGlideImageLoader())
                            .isShowSaveButton(false)
                            .show();
                }
            });
        }

    }

    @Override
    public void onFailure(int requestCode) {

    }
}
